
#include "pass.h"
#include "ui_pass.h"

pass::pass(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::pass)
{
    ui->setupUi(this);
    t="";
    wid=450;
    isPathfinding=false;
    isPosition=false;
    star=false;
    success=false;
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));

   ui->label_5->setText("当前得分为：");
   isret=false;

}

pass::~pass()
{
    delete ui;
}
 static int num;
 static int score=0;
void pass::timerEvent(QTimerEvent *)
{
    if(isret){
        return;
    }
    if(num>=0)
    {
        num--;

        ui->label_4->setText(secsToTime(num));

    }
    else
    {
        success=true;
        update();
        killTimer(time1);
        ui->label_4->setText(secsToTime(0));

    }
}

QString pass::secsToTime(int s)
{
    int m=0;
    s%=3600;
    m=s/60;
    s%=60;
    QString str=QString::number(m)+"分:"+QString::number(s)+"秒";
    return str;

}



Node pass::getStartPoint(){
    return this->startPoint;
}
void pass::setStartPoint(Node node){
    this->startPoint=node;
}

bool pass::isEven(int sum){
    if(sum%2){
        return false;
    }else{
        return true;
    }
}

void pass::on_Return_clicked()
{
    isret=true;
    emit ret();
}

void pass::on_btn1_clicked()
{
   t+="1";
   ui->textEdit->setText(t);
}

void pass::on_btn2_clicked()
{
    t+="2";
    ui->textEdit->setText(t);
}

void pass::on_btn3_clicked()
{
    t+="3";
    ui->textEdit->setText(t);
}

void pass::on_btn4_clicked()
{
    t+="4";
    ui->textEdit->setText(t);
}

void pass::on_btn5_clicked()
{
    t+="5";
    ui->textEdit->setText(t);
}

void pass::on_btn6_clicked()
{
    t+="6";
    ui->textEdit->setText(t);
}


void pass::on_btn7_clicked()
{
    t+="7";
    ui->textEdit->setText(t);
}

void pass::on_btn8_clicked()
{
    t+="8";
     ui->textEdit->setText(t);
}

void pass::on_btn9_clicked()
{
    t+="9";
    ui->textEdit->setText(t);
}

void pass::on_btn0_clicked()
{
    t+="0";
    ui->textEdit->setText(t);
}

void pass::on_Cancle_2_clicked()
{
   t= t.mid(0,t.size()-1);
    ui->textEdit->setText(t);
}


static int endi,endj;

void pass::on_Confirm_2_clicked()
{
    if(ui->textEdit->toPlainText()!=""){
        n=ui->textEdit->toPlainText().toInt();
        ui->textEdit->setText("");
        t="";

        if(!isEven(n)){
            n++;
        }
         ui->label->setText(QString("迷宫大小：%1*%2").arg(n).arg(n));
        if(n<6){
            n=6;
            ui->label->setText("已超过迷宫最小生成范围，自动生成迷宫大小：6*6");
        }
        if(n>190){
            n=190;
            ui->label->setText("已超过迷宫最大生成范围，自动生成迷宫大小：190*190");
        }
        ui->promt->setText("Have a good day!");
        wid=1500/(n+4);
        maze.init(n);
        maze.mapBuid();
        startPoint=maze.map[2][2];
        endi=n,endj=n;
        star=true;
        num=480;
        time1= startTimer(1000);
        update();
        ui->label_5->setText("当前得分为：0");
    }
}






//随机生成地图
//void pass::on_pushButton_clicked()
//{
//    n=(qrand()%45+6)*2;
//    ui->label->setText(QString("迷宫大小：%1*%2").arg(n).arg(n));
//    ui->promt->setText("Have a good day!");
//    wid=1500/(n+4);
//    maze.init(n);
//    maze.mapBuid();
//    startPoint=maze.map[2][2];
//    star=true;
//    success=false;
//    time.setHMS(0,0,0,0);//设置初始值
//    ui->time->setText(time.toString("hh:mm:ss"));
//    timer->start();
//    update();

//}

static int starti=2,startj=2;
//static int endi=n,endj=n;

//重写paintEvent函数
void pass::paintEvent(QPaintEvent *)
{
    if(star){
    QPainter painter(this);
    QPixmap pixmap;
    pixmap.load(":/rec/260.png");
    //设置反锯齿模式
     painter.setRenderHint(QPainter::Antialiasing, true);
     if(success){
         pixmap.load(":/rec/success.jpeg");
         painter.drawPixmap(10, 10,1400 ,1200 ,pixmap);
         painter.setPen(Qt::red);
         QFont font;
         font.setPointSize(24);
         painter.setFont(font);
         QString str1=QString::number(score);
         QString str2="最终得分为："+str1;
          painter.drawText(920,320,str2);

     }else{
         for(int i=0;i<n+3;i++){
             for(int j=0;j<n+3;j++){
                 if(maze.map[i][j].flag==-1||maze.map[i][j].flag==0){
                     //墙
                     pixmap.load(":/rec/wall.jpeg");
                     painter.drawPixmap(i*wid, j*wid, wid, wid,pixmap);
                 }else{
                    painter.setBrush(QColor(255, 255, 255));
                    painter.drawRect(i*wid, j*wid, wid, wid);
                 }
             }
         }
         pixmap.load(":/rec/home.jpeg");
         painter.drawPixmap(starti*wid, startj*wid, wid, wid,pixmap);
         pixmap.load(":/rec/end.jpg");
        // endi=n,endj=n;
         painter.drawPixmap(endi*wid, endj*wid, wid, wid,pixmap);
         pixmap.load(":/rec/260.png");
         //寻路绘图
         if(isPathfinding){
              painter.setBrush(QColor(0, 255, 0));
              QList<Node> L=maze.getRoute();
              for(int i=1;i<L.size()-1;i++){
                      painter.drawRect(L[i].x*wid, L[i].y*wid, wid, wid);
              }
              painter.drawPixmap(startPoint.x*wid, startPoint.y*wid, wid, wid,pixmap);
              isPathfinding=false;
         }
         if(isPosition){
             painter.drawPixmap(startPoint.x*wid, startPoint.y*wid, wid, wid,pixmap);
             isPosition=false;
         }

     }


}
}

////寻路
//void pass::on_Auto_clicked()
//{
//    if(star){
//        //初始化列表
//        maze.setB(true);
//        maze.mapSearch(startPoint,maze.map[n][n]);
//        isPathfinding=true;
//        update();
//    }
//}


//按键操作
void pass::keyPressEvent(QKeyEvent *e){
    //向上移动
    if(e->key()==0x57||e->key()==0x01000013){
        if(maze.map[startPoint.x][startPoint.y-1].flag==1){
            startPoint.y--;
        }

    }
    //向右移动
    if(e->key()==0x44 ||e->key()==0x01000014){
        if(maze.map[startPoint.x+1][startPoint.y].flag==1){
            startPoint.x++;
        }
    }
    //向下移动
    if(e->key()==0x53 ||e->key()==0x01000015){
        if(maze.map[startPoint.x][startPoint.y+1].flag==1){
            startPoint.y++;
        }
    }
    //向左移动
    if(e->key()== 0x41 ||e->key()==0x01000012){
        if(maze.map[startPoint.x-1][startPoint.y].flag==1){
            startPoint.x--;
        }
    }
    if(startPoint.x==endi &&startPoint.y==endj){
        score++;
        QString str1=QString::number(score);
        QString str2="当前得分为："+str1;
        ui->label_5->setText(str2);
        wid=1500/(n+4);
        maze.init(n);
        num=num+30;
        maze.mapBuid();
        starti=endi,startj=endj;
        setEnd(n);
        startPoint=maze.map[starti][startj];
        star=true;
        success=false;
        update();

    }else{
        success=false;
    }
    isPosition=true;
    update();

}


//设置终点
void pass::setEnd(int n){

     int i=qrand()%n;
     int j=qrand()%n;
     if(i==starti&&j==startj){
         setEnd( n);
     }else{
     if(maze.map[i][j].flag==1){
         endi=i;
         endj=j;
     }else{
         setEnd( n);
     }
     }


}


